<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport"
            content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <style>
            html,
            body {
                margin: 0;
                width: 100%;
                height: 100%;
                background: url("") center center;
                background-repeat: no-repeat;
                background-size: cover;
                object-fit: fill;
            }
        </style>
    </head>
  <body>
    <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
    
    <div id="bg"
        style="width:100%;height: 100%;display: flex;flex-direction: row;align-items: center;justify-content: center;">
        <div id="player"></div>
    </div>

    <script>
      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement('script');

      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '360',
          width: '640',
          videoId: 'M7lc1UVf-VE',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          },
          playerVars: {
                'controls': 0,
                'playsinline': 1,
                'autoplay': 1
            }
        });

        // setInterval(playVideo, 3000);
      }

      var isPlay = false
      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        // event.target.playVideo();
        setInterval(onStartPlaying, 3000);
      }

      function onStartPlaying(){
        try{
            var videoDiv = document.getElementsByTagName('iframe')[0]
            var videoWidht = videoDiv.style.width
            var videoHeight = videoDiv.style.height
            window.YouTuBeInterface.onStartPlaying("", videoWidht, videoHeight);
        }catch (err) {}
      }

      function playVideo(){
        alert("播放")
        player.setVolume(100);
            player.playVideo();
        // player.playVideo();
      }

      // 5. The API calls this function when the player's state changes.
      //    The function indicates that when playing a video (state=1),
      //    the player should play for six seconds and then stop.
      var done = false;
      function onPlayerStateChange(event) {
          console.log("event.data "+event.data)
          if(event.data==1){
            isPlay = true
              console.log("ssss")
          }else{
              isPlay = false
          }
        if (event.data == YT.PlayerState.PLAYING && !done) {
          setTimeout(stopVideo, 6000);
          done = true;
        }
    
      }
      function stopVideo() {
        player.stopVideo();
      }

      
    </script>
  </body>
</html>